home *** CD-ROM | disk | FTP | other *** search
- /* File: Recorder.c
-
- MPW Tool for recording serial line traffic
-
- */
-
-
- #include <Types.h>
- #include <QuickDraw.h>
- #include <CursorCtl.h>
-
- #include <CRMIntf.h>
- #include <CTBUtils.h>
- #include <CMIntf.h>
- #include <FTIntf.h>
- #include <TMIntf.h>
-
- #include <StdIO.h>
- #include <StdLib.h>
-
-
- #define CommToolBoxTrap 0x8B
- #define UnimplementedTrap 0x9F
- #define Check(err,str) { \
- OSErr errXYZZY; \
- if (( errXYZZY = ( err )) != noErr ) \
- fprintf ( stderr, "Error %d calling %s\n", errXYZZY, str ); \
- }
- #define INCONFIGSTR "Baud 9600 dataBits 8 Parity None StopBits 1 Port \"Modem Port\"" \
- "Handshake None HoldConnection False RemindDisconnect False"
- #define OUTCONFIGSTR "Baud 9600 dataBits 8 Parity None StopBits 1 Port \"Printer Port\"" \
- "Handshake None HoldConnection False RemindDisconnect False"
-
- #define BUF_SIZE 1024
-
- short inProcID;
- short outProcID;
- ConnHandle inputStream;
- ConnHandle outputStream;
- char inBuffer [ BUF_SIZE ];
- char outBuffer [ BUF_SIZE ];
- CMFlags inFlags;
- CMStatFlags inStatFlags;
-
- /* Is the Comm Toolbox actually installed ?? */
- Boolean IsCTBInstalled ( ) {
- return NGetTrapAddress ( UnimplementedTrap, OSTrap ) !=
- NGetTrapAddress ( CommToolBoxTrap, OSTrap );
- }
-
- short InitAll ( void ) {
- OSErr err;
-
- InitGraf ( &qd.thePort );
- /*
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs ( NULL );
- InitCursor ();
- */
-
- InitCursorCtl ( NULL );
-
- if ( !IsCTBInstalled ) {
- fprintf ( stderr, "Comm Toolbox not installed!\n" );
- return 1;
- }
-
- /* Load up the Communications Toolbox */
- (void) InitCTBUtilities ();
- (void) InitCRM ();
-
- err = InitTM ();
- if ( err == tmNoTools ) {
- fprintf ( stderr, "No terminal tools found\n" );
- return 2;
- }
-
- err = InitCM ();
- if ( err == cmNoTools ) {
- fprintf ( stderr, "No connection tools found\n" );
- return 2;
- }
-
- err = InitFT ();
- if ( err == ftNoTools ) {
- fprintf ( stderr, "No file transfer tools found\n" );
- return 2;
- }
-
- return 0;
- }
-
-
- void ExitProc ( void ) {
-
- /* Close the connection and dispose of the connection record */
- if ( inputStream != NULL ) {
- Check ( CMClose ( inputStream, false, NULL, -1, false ), "CMCLose - input" );
- CMDispose ( inputStream );
- }
-
- if ( outputStream != NULL ) {
- Check ( CMClose ( outputStream, false, NULL, -1, false ), "CMCLose - output" );
- CMDispose ( outputStream );
- }
- }
-
- ConnHandle InitStream ( Boolean isIn, short *procID ) {
- CMBufferSizes bSize;
-
- /* Open a connection tool */
- bSize [ cmDataIn ] = 0; bSize [ cmDataOut ] = 0;
- bSize [ cmCntlIn ] = 0; bSize [ cmCntlOut ] = 0;
- bSize [ cmAttnIn ] = 0; bSize [ cmAttnOut ] = 0;
- bSize [ cmRsrvIn ] = 0; bSize [ cmRsrvOut ] = 0;
- if ( isIn )
- bSize [ cmDataIn ] = BUF_SIZE;
- else
- bSize [ cmDataOut ] = BUF_SIZE;
- *procID = CMGetProcID ( "\pSerial" );
- return CMNew ( *procID, cmQuiet + cmNoMenus, bSize, 0L, 0L );
- }
-
-
-
- int main ( int argc, char *argv[] ) {
- short err;
- long cnt;
-
- if ( err = InitAll ( ) != 0 )
- exit ( err );
-
- atexit ( ExitProc );
-
- /* Open a connection tool */
- inputStream = InitStream ( true, &inProcID );
- if ( inputStream == NULL ) {
- fprintf ( stderr, "Cannot create input handle\n" );
- return 3;
- }
- /* Open a connection tool */
- outputStream = InitStream ( false, &outProcID );
- if ( inputStream == NULL ) {
- fprintf ( stderr, "Cannot create out handle\n" );
- return 3;
- }
-
-
-
- /* Configure the connection */
- Check ( CMSetConfig ( inputStream, INCONFIGSTR ), "CMSetConfig - In" );
- Check ( CMOpen ( inputStream, false, NULL, -1 ), "CMOpen - In" );
- Check ( CMListen ( inputStream, false, NULL, -1 ), "CMListen - In" );
-
- Check ( CMSetConfig ( outputStream, OUTCONFIGSTR ), "CMSetConfig - Out" );
- Check ( CMOpen ( outputStream, false, NULL, -1 ), "CMOpen - Out" );
- Check ( CMListen ( outputStream, false, NULL, -1 ), "CMListen - Out" );
-
- cnt = 0;
- while ( true ) {
- CMBufferSizes bSize;
-
- cnt++;
- if ( cnt % 32 == 0 ) {
- CMIdle ( inputStream );
- CMIdle ( outputStream );
- SpinCursor ( 1 );
- }
-
- /* read data and print it out */
- bSize [ cmDataIn ] = 0;
- Check ( CMStatus ( inputStream, bSize, &inStatFlags ), "CMStatus" );
- if ( bSize [ cmDataIn ] > 0 ) {
- Check ( CMRead ( inputStream, inBuffer, &bSize [ cmDataIn ],
- cmData, false, NULL, -1, &inFlags ), "CMRead" );
- Check ( CMWrite ( outputStream, inBuffer, &bSize [ cmDataIn ],
- cmData, false, NULL, -1, inFlags ), "CMWrite" );
- }
-
-
- }
-
- return 0;
- }
-